//
//  ActivityViewController.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/14.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityDetailController.h"
#import "ActivityViewCell.h"
#import "ActionModel.h"
#import "ActivityStore.h"

@interface ActivityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSArray *dataArr;

@end

@implementation ActivityViewController

- (NSArray *)dataArr{
    
    if (!_dataArr) {
        _dataArr = [[NSArray alloc]init];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self loadData];
}     
- (void)loadData{
    
    MJWeakSelf
    ActivityStore *store = [[ActivityStore alloc] init];
    [store getActivityListWithCustid:[UserDefaultsManager sharedInstance].user_id Success:^(NSArray *listArr) {
        
        weakSelf.dataArr = listArr;
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
        
    }];
}

- (void)initCSS{
    
    [self setNavBarWithTitle:@"活动专区"];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"ffffff"];

    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [_tableView registerClass:[ActivityViewCell class] forCellReuseIdentifier:@"ActivityViewCellID"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > 0) {
        ActionModel *model = self.dataArr[indexPath.section];
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.type]]];
         return image.size.height/image.size.width * (SCREEN_WIDTH - 28);
    }
    return 134;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ActivityViewCellID"];
    if (self.dataArr.count > 0) {
        ActionModel *model = self.dataArr[indexPath.section];
        cell.model = model;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ActionModel *model = self.dataArr[indexPath.section];
    ActivityDetailController *VC = [[ActivityDetailController alloc] init];
    VC.imagepath = model.imgPath;
    [self.navigationController pushViewController:VC animated:YES];
}


@end
